package 题目集.hash.哈希去重;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * https://leetcode.cn/problems/reduction-operations-to-make-the-array-elements-equal/
 */
public class demo05_使数组元素相等的减少操作次数 {

    public int reductionOperations(int[] nums) {
        Map<Integer, int[]> map = new HashMap<>();
        for (int num : nums) {
            int[] cnt = map.computeIfAbsent(num, v -> new int[]{v, 0});
            cnt[1]++;
        }
        List<int[]> collect = map.values().stream()
                .sorted((a, b) -> a[0] - b[0])
                .collect(Collectors.toList());
        int preCnt = 0;
        int res = 0;
        for (int[] ints : collect) {
            res += preCnt * ints[1];
            preCnt++;
        }
        return res;
    }

    @Test
    public void test() {
        int[] arr = {1, 1, 2, 2, 3};
        int i = reductionOperations(arr);
        System.out.println(i);
    }
}
